package com.gameloft.futbol.element;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import com.gameloft.input.Pointer;
import com.gameloft.futbol.element.screen.MainScreen;
import com.gameloft.futbol.Element;
import com.gameloft.futbol.fCanvas;
import com.gameloft.futbol.Util;


public abstract class Viewport extends Element
{
	
	static Image background;
	int boxX, boxY, boxW = 100, boxH = 50;
	static boolean clearBox;
	static int highlightCount = 0;
	
	
	public void update(long elapsed)
	{
		if (background == null)
		{
			background = Util.loadImage("/background.png");
			int newSize = (w <= h) ? w*2/7 : h*2/7;
			if (background.getWidth() > newSize)
			{
				int[] argb = Util.getImageRGB(background);
				background = Util.resizeImage(argb, background.getWidth(), background.getHeight(), newSize, newSize);
			}
		}
		
		if (clearBox)
		{
			invalidateRect(boxX, boxY, boxW, boxH);
			clearBox = false;
		}
		
		Button b = ((MainScreen)parent).getHighlightedButton();
		if (b != null)
		{
			highlightCount++;
			if (highlightCount >= 10)
			{
				int centerX = b.getX() + b.getW()/2;
				boxX = (centerX-boxW/2 < 10) ? 10 : centerX-boxW/2;
				boxX = (boxX+boxW > fCanvas.getScreenWidth()-10) ? fCanvas.getScreenWidth()-10-boxW : boxX;
				boxY = (b.getY() < fCanvas.getScreenHeight()/2) ? b.getY()+b.getH()+5 : b.getY()-boxH-5;
				invalidateRect(boxX, boxY, boxW, boxH);
				clearBox = true;
			}
		}
		else highlightCount = 0;
	}
	
	public void paint(Graphics g, int rectX, int rectY, int rectW, int rectH)
	{
		Util.pushClip(g, rectX, rectY, rectW, rectH);
		
		int bkgWidth = background.getWidth();
		int bkgHeight = background.getHeight();
		
		for (int drawY = y; drawY < y+h; drawY += bkgHeight)
		for (int drawX = x; drawX < x+w; drawX += bkgWidth)
		{
			if ( (drawX+bkgWidth >= rectX) && (drawX < rectX+rectW) &&
					 (drawY+bkgHeight >= rectY) && (drawY < rectY+rectH) )
			{
				g.drawImage(background, drawX, drawY, 0);
			}
		}
		
		Button b = ((MainScreen)parent).getHighlightedButton();
		if ((b != null) && (highlightCount >= 10)) paintBox(g);
		
		Util.popClip(g);
	}
	
	public void paintBox(Graphics g)
	{
		if (boxY < fCanvas.getScreenHeight()/2)
		{
			g.setColor(0xFFFFFF);
			g.fillRoundRect(boxX, boxY+10, boxW, boxH-10, 15, 15);
			int offsX = 40;
			if (boxX == 10)
				offsX = 25;
			else if (boxX == fCanvas.getScreenWidth() - 10 - boxW)
				offsX = 60;
			g.fillTriangle(boxX+offsX,boxY+10,boxX+5+offsX,boxY,boxX+8+offsX,boxY+10);
			g.setColor(0x000000);
			String caption = ((MainScreen)parent).getHighlightedButton().getCaption();
//			g.drawString(caption, boxX + boxW/2, boxY+10 + (boxH-10)/2 + 5, Graphics.HCENTER);
		}
		else
		{
			g.setColor(0xFFFFFF);
			g.fillRoundRect(boxX, boxY, boxW, boxH-10, 15, 15);
			int offsX = 40;
			if (boxX == 10)
				offsX = 25;
			else if (boxX == fCanvas.getScreenWidth() - 10 - boxW)
				offsX = 60;
			g.setColor(0xFFFFFF);
			g.fillTriangle(boxX+offsX,boxY+boxH-10,boxX+5+offsX,boxY+boxH,boxX+8+offsX,boxY+boxH-10);
			g.setColor(0x000000);
			String caption = ((MainScreen)parent).getHighlightedButton().getCaption();
//			g.drawString(caption, boxX + boxW/2, boxY+10 + (boxH-10)/2 - 5, Graphics.HCENTER);
		}
	}
	
}